<template>
	<view>
		<!-- 搜索框 -->
		<view class="search-box">
			<uni-search-bar @input="input" cancelButton="none" radius="20" focus="true"></uni-search-bar>
		</view>
		<!-- 搜索结果的列表 -->
		<view class="search-results-list" v-if="searchList.length>0">
			<view class="search-results-item" v-for="(item,i) in searchList" :key="i" @click="clickitem(item.goods_id)">
				<view class="search-results-item-name">{{item.goods_name}}</view>
				<uni-icons type="arrowright"></uni-icons>
			</view>

		</view>
		<!-- 搜索历史 -->
		<view class="history-container" v-else>
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons type="trash" size="18" @click="delhistory"></uni-icons>
			</view>
			<view class="header-keywords">
				<uni-tag :text="item" v-for="(item,i) in historyKeywords" :key="i" @click="gotoGoodsList(item)">
				</uni-tag>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				timeid: null,
				key: '',
				searchList: [],
				historyKeywords: []
			};
		},
		onLoad() {
			this.historyKeywords = JSON.parse(uni.getStorageSync('kw') || '[]')
		},
		methods: {
			input(val) {
				clearTimeout(this.timeid)
				this.timeid = setTimeout(() => {
					// console.log()
					// console.log(typeof(val.trim()))
					this.key = val
					this.getsearch()

				}, 1000)
			},
			async getsearch() {
				// if (this.key.trim().length === 0) {
				if (!this.key.trim()) {
					this.searchList = []
					return
				}
				const {
					data: res
				} = await uni.$http.get('/goods/qsearch', {
					query: this.key
				})
				this.searchList = res.message
				this.saveSearchHistory()

			},
			clickitem(id) {
				uni.navigateTo({
					url: `/subpkg/goods_detail/goods_detail?goods_id=` + id
				})
			},
			saveSearchHistory() {
				const index = this.historyKeywords.indexOf(this.key)
				if (index < 0) {
					this.historyKeywords.unshift(this.key)
				} else {
					this.historyKeywords.splice(index, 1)
					this.historyKeywords.unshift(this.key)
				}
				uni.setStorageSync('kw', JSON.stringify(this.historyKeywords))
			},
			delhistory() {
				this.historyKeywords = []
				uni.removeStorageSync('key')
			},

			// 点击跳转到商品列表页面
			gotoGoodsList(kw) {
				uni.navigateTo({
					url: '/subpkg/goods_list/goods_list?query=' + kw
				})
			}
		}
	}
</script>

<style lang="scss">
	.search-box {
		background-color: #c00000;
		position: sticky;
		top: 0;
		z-index: 9;
	}

	.search-results-list {
		padding: 0 20rpx;

		.search-results-item {
			display: flex;
			justify-content: space-between;
			align-items: center;
			border-bottom: 1px solid #efefef;
			height: 90rpx;

			.search-results-item-name {
				font-size: 24rpx;
				// 文字不允许换行
				white-space: nowrap;
				// 溢出的部分隐藏
				overflow: hidden;
				// 文本溢出后，使用。。。代替
				text-overflow: ellipsis;
				margin-right: 3px;
			}
		}
	}

	.history-container {
		.history-title {
			display: flex;
			justify-content: space-between;
			padding: 0 20rpx;
			border-bottom: 1rpx solid #efefef;
		}

		.header-keywords {
			display: flex;
			flex-wrap: wrap;

			uni-tag {
				margin: 10rpx;
			}
		}
	}
</style>
